package com.leafrest.core.constant;

public enum ErrorCode {
	SUCCESS(1, "操作成功!"), 
	FAILED(2, "服务器未知错误!"), 
	CUSTOM(3, "自定义错误"), 
	INTERNAL_ERROR(1000, "系统内部错误,请联系系统管理员!"), 
	SIGNATURE_ACCREDIT_FAILED(1001, "签名认证失败!"), 
	SERVER_RESPONSE_BUSY(1002, "系统繁忙，请稍后再试!"), 
	SERVER_RESPONSE_TIMEOUT(1003, "请求超时，请稍后再试!"), 
	SERVER_RESPONSE_INVALIDURL(1004, "API地址不存在!"), 
	PARAMTER_REQUIRE(2000, "参数<{}>不允许为空!"), 
	PARAMTER_TOO_SHORT(2001, "参数<{}>长度至少要大于{}个字符!"), 
	PARAMTER_TOO_LONG(2002, "参数<{}>长度不能大于{}个字符!"), 
	PARAMTER_FORMAT_ILLEGAL(2003, "参数<{}>不是合法的<{}>类型!");
//	//以下是自定义错误
//	ACCOUNT_MUE_REQUIRE(3000, "参数<mobile、user_name、email>必须有一个不能为空!"),
//	ACCOUNT_MOBILE_EXIST(3001, "手机号<{}>已经被注册!"),
//	ACCOUNT_USERNAME_FORMAT(3002, "用户名必须是字母、数字、“_”组成的4-10位字符!"),
//	ACCOUNT_USERNAME_EXIST(3003, "用户名<{}>已经被注册!"),
//	ACCOUNT_EMAIL_EXIST(3004, "邮箱<{}>已经被注册!"),
//	ACCOUNT_PASSWORD_FORMAT(3005, "密码必须有字母和数字组合且≥6位≤16位");
	
	private int code;
	private String msg;
	private Object args[];

	// 构造方法
	private ErrorCode(int code, String msg) {
		this.code = code;
		this.msg = msg;
	}

	public int getCode() {
		return code;
	}

	public ErrorCode setCode(int code) {
		this.code = code;
		return this;
	}

	public String getMsg() {
		if (args != null && args.length > 0) {
			String msg = this.msg;
			for (Object arg : args) {
				if(msg.indexOf("{}") != -1) {
					msg = msg.substring(0, msg.indexOf("{}")).concat(arg.toString()).concat(msg.substring(msg.indexOf("{}") + 2));
				}
			}
			return msg;
		} else {
			return this.msg;
		}
	}

	public ErrorCode setMsg(String msg) {
		this.msg = msg;
		return this;
	}

	public Object[] getArgs() {
		return args;
	}

	public ErrorCode setArgs(Object... args) {
		this.args = args;
		return this;
	}
}
